#include "zonesitmgr.h"
#include "timefuncdef.h"
#include "config_mgr.h"
#include "proto_svr.h"
#include "global_server.h"
#include "common_def.h"
#include <sys/time.h>


ZoneSitMgr::ZoneSitMgr()
{}

ZoneSitMgr::~ZoneSitMgr()
{
	
}



void ZoneSitMgr::reload()
{
	/*for(std::map<int, Rank *>::iterator i = m_ranks.begin(); i != m_ranks.end(); ++i)
	{
		i->second->init();
	}*/
}

void ZoneSitMgr::init(base::FileConfig &confMgr, base::Logger *logger, GlobalServer *pServer)
{
	try
	{
		m_pRollLog = logger;


		m_pDbMgr = new DBMgr;
		m_pDbMgr->init_db_ar(confMgr, logger, true); //只存redis

		m_pServer = pServer;

		loadFromCache();
		
	}
	catch(std::runtime_error &e)
	{
		m_pRollLog->error("[%s:%d] %s", __FILE__, __LINE__, e.what());
	}
	catch(...)
	{
		m_pRollLog->error("[%s:%d] unkown exception", __FILE__, __LINE__);
	}
}

void ZoneSitMgr::loadFromCache()
{

	const std::map<int, CsvConfig::ByChooseSeat> byChooseSeatMap = CsvConfigMgr::getInstance().getByChooseSeatMap();
	for(std::map<int, CsvConfig::ByChooseSeat>::const_iterator it = byChooseSeatMap.begin(); it != byChooseSeatMap.end(); it++)
	{
		int nZoneNum = 0;
		int nZoneId = it->second.get_id();
		QueryParam qp;
		qp.tableName = CACHE_GLZONESITINFO;
		qp.userid = nZoneId;
		qp.userIdFiledName = "zone";
		qp.keyFieldName = "sit";
		//qp.key = 

		DataVector res;
		m_pDbMgr->get_cache_redis(qp, res);
		for(DataVector::const_iterator itr = res.begin(); itr != res.end(); ++itr)
		{
			msgpack::unpacked unpack;
			msgpack::unpack(&unpack, itr->data(), itr->length());

			msgpack::object obj = unpack.get();
			std::stringstream ss;
			ss<<obj;

			Json::Reader reader;
			Json::Value value;
			if(reader.parse(ss.str(), value))
			{
				int nSit = 0;
				int nNum = 0;
				int nUpdated = 0;
				int robot = 0;
				int nSetPwd = 0;
				string strPwd = "";
				if(value.isMember("sit"))
				{
					nSit = value["sit"].asInt();
				}
				else
				{
					continue;
				}
				if(value.isMember("num"))
				{
					nNum = value["num"].asInt();
				}
				if(value.isMember("updated"))
				{
					nUpdated = value["updated"].asInt();
				}
				if (value.isMember("robot"))
				{
					robot = value["robot"].asInt();
				}
				if (value.isMember("pwd"))
				{
					strPwd = value["pwd"].asString();
				}
				if (value.isMember("set_pwd"))
				{
					nSetPwd = value["set_pwd"].asInt();
				}
				
				if (robot == 1) //机器人房间重启不生效
				{
					nNum = 0;
					robot = 0;
				}

				ZoneSitData zsData;
				zsData.playerCnt = nNum;
				zsData.sit = nSit;
				zsData.zone = nZoneId;
				zsData.updateTime = nUpdated;
				zsData.robot = robot;
				zsData.strPwd = strPwd;
				zsData.nSetPwd = nSetPwd;
				m_mapZone2SitInfo[nZoneId][nSit] = zsData;
				nZoneNum += nNum;
			}
		}
		m_mapZone2Num[nZoneId] = nZoneNum;
	}
	
}

void ZoneSitMgr::saveAll2Cache()
{

	for(std::map<int, map<int, ZoneSitData> >::iterator it = m_mapZone2SitInfo.begin(); it != m_mapZone2SitInfo.end(); it++)
	{
		for(map<int, ZoneSitData>::iterator itt = it->second.begin(); itt != it->second.end(); ++itt)
		{

			QueryParam param;
			param.tableName = CACHE_GLZONESITINFO;
			param.keyFieldName = "sit";
			param.key = itt->second.sit;
			param.userIdFiledName = "zone";
			param.userid = it->first;
		    
		    msgpack::sbuffer sbuf;
			msgpack::packer<msgpack::sbuffer> pker(&sbuf);
			pker.pack_map(5);
			pker.pack(std::string("zone"));
			pker.pack(it->first);
			pker.pack(std::string("sit"));
			pker.pack(itt->second.sit);
			pker.pack(std::string("num"));
			pker.pack(itt->second.playerCnt);
			pker.pack(std::string("updated"));
			pker.pack(itt->second.updateTime);
			pker.pack(std::string("robot"));
			pker.pack(itt->second.robot);
			
			m_pDbMgr->set_cache_redis(param,string(sbuf.data(), sbuf.size()));
		}
	}
	
}

void ZoneSitMgr::save2Cache(int nZone, int nSit, int nNum, int nUpdated, int robot)
{

	QueryParam param;
	param.tableName = CACHE_GLZONESITINFO;
	param.keyFieldName = "sit";
	param.key = nSit;
	param.userIdFiledName = "zone";
	param.userid = nZone;
    
    msgpack::sbuffer sbuf;
	msgpack::packer<msgpack::sbuffer> pker(&sbuf);
	pker.pack_map(5);
	pker.pack(std::string("zone"));
	pker.pack(nZone);
	pker.pack(std::string("sit"));
	pker.pack(nSit);
	pker.pack(std::string("num"));
	pker.pack(nNum);
	pker.pack(std::string("updated"));
	pker.pack(nUpdated);
	pker.pack(std::string("robot"));
	pker.pack(robot);
	m_pDbMgr->set_cache_redis(param,string(sbuf.data(), sbuf.size()));
}

void ZoneSitMgr::save2Cache(ZoneSitData &data)
{
	QueryParam param;
	param.tableName = CACHE_GLZONESITINFO;
	param.keyFieldName = "sit";
	param.key = data.sit;
	param.userIdFiledName = "zone";
	param.userid = data.zone;
	data.updateTime = time(NULL);
    
    msgpack::sbuffer sbuf;
	msgpack::packer<msgpack::sbuffer> pker(&sbuf);
	pker.pack_map(7);
	pker.pack(std::string("zone"));
	pker.pack(data.zone);
	pker.pack(std::string("sit"));
	pker.pack(data.sit);
	pker.pack(std::string("num"));
	pker.pack(data.playerCnt);
	pker.pack(std::string("updated"));
	pker.pack(data.updateTime);
	pker.pack(std::string("robot"));
	pker.pack(data.robot);
	pker.pack(std::string("pwd"));
	pker.pack(data.strPwd);
	pker.pack(std::string("set_pwd"));
	pker.pack(data.nSetPwd);
	m_pDbMgr->set_cache_redis(param,string(sbuf.data(), sbuf.size()));
}

void ZoneSitMgr::initZoneData(const int &nZone, const int &nTotal)
{
	try
	{
		bool bHaveZoneInfo = false;
		int nZoneNum = 0;
		if (m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
		{
			bHaveZoneInfo = true;
			nZoneNum = m_mapZone2SitInfo[nZone].size();
		}
		else
		{
			map<int, ZoneSitData> mapTmp;
			m_mapZone2SitInfo[nZone] = mapTmp;
		}
		if (nZoneNum < nTotal)
		{
			for (int i = 1; i <= nTotal; i++)
			{
				bool bInsert = bHaveZoneInfo == true ? false : true;
				if (!bInsert)
				{
					if (m_mapZone2SitInfo[nZone].find(i) == m_mapZone2SitInfo[nZone].end())
					{
						bInsert = true;
					}
				}
				if (bInsert)
				{
					ZoneSitData zsData;
					zsData.zone = nZone;
					zsData.sit = i;
					zsData.updateTime = time(NULL);
					zsData.playerCnt = 0;
					zsData.robot = 0;
					m_mapZone2SitInfo[nZone][i] = zsData;
				}
			}
		}
		else if (nZoneNum > nTotal)
		{	
			map<int, ZoneSitData> mapTmp = m_mapZone2SitInfo[nZone];
			map<int, ZoneSitData>::iterator iter = mapTmp.begin();
			for (; iter != mapTmp.end(); ++iter)
			{
				if (iter->first < 1 || iter->first > nTotal)
				{
					m_mapZone2SitInfo[nZone].erase(iter->first);
					QueryParam qp;
					qp.tableName = CACHE_GLZONESITINFO;
					qp.userIdFiledName = "zone";
					qp.userid = nZone;
					qp.keyFieldName = "sit";
					qp.key = iter->first;
					m_pDbMgr->remove_cache_redis(qp);
					m_pRollLog->debug("ZoneSitMgr::initZoneData delete zone info[zone:%d, sit:%d]", nZone, iter->first);
				}
			}
			m_mapZoneRobot[nZone].clear(); //机器人房间号
			m_mapRobotRule[nZone] = 0;//当前机器人规则
		}	
	}
	catch(...)
	{
		m_pRollLog->error("ZoneSitMgr::initZoneData exception.....");
	}
}


bool ZoneSitMgr::initRobotZoneRule(const int &nZone, const int &nRuleId)
{	
	try
	{
		if (m_mapRobotRule.find(nZone) == m_mapRobotRule.end())
		{
			m_mapRobotRule[nZone] = 0;
		}
		if (m_mapRobotRule[nZone] != nRuleId)
		{
			m_pRollLog->debug("ZoneSitMgr::initRobotZoneRule parameter[zone:%d, pre configure:%d, now configure:%d]", nZone, m_mapRobotRule[nZone], nRuleId);
			m_mapRobotRule[nZone] = nRuleId;
			if (m_mapZoneRobot.find(nZone) != m_mapZoneRobot.end())
			{
				for (int i = 0; i < m_mapZoneRobot[nZone].size(); i++)
				{
					int sit = m_mapZoneRobot[nZone][i];
					if (m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
					{
						if (m_mapZone2SitInfo[nZone].find(sit) != m_mapZone2SitInfo[nZone].end())
						{
							time_t t = time(NULL);
							m_mapZone2SitInfo[nZone][sit].playerCnt = 0;
							m_mapZone2SitInfo[nZone][sit].robot = 0;
							m_mapZone2SitInfo[nZone][sit].updateTime = t;
							m_mapZone2Num[nZone] -= 4;
							this->save2Cache(m_mapZone2SitInfo[nZone][sit]);
							m_pRollLog->debug("ZoneSitMgr::initRobotZoneRule rule different reset robot zone[zone:%d, sit:%d, rule:%d]", nZone, sit, nRuleId);
						}
					}
				}
			}
			else
			{
				vector<int> vecTmp;
				m_mapZoneRobot[nZone] = vecTmp;
			}
			m_mapZoneRobot[nZone].clear();
			return true;
		}
	}
	catch(...)
	{
		m_pRollLog->error("ZoneSitMgr::initRobotZoneRule exception[zone:%d, rule:%d]", nZone, nRuleId);
	}
	return false;
}

int ZoneSitMgr::checkRobotZone(const int &nZone)
{
	try
	{
		bool bChange = false;
		vector<int> vecFreeZone; //空闲的选座房间
		string strRobotCtrl = "";
		vector< vector<int> > vecRobotConfig;
		//1.获取配置
		CsvConfig::ByChooseSeat config;
		if(!CsvConfigMgr::getInstance().findByChooseSeatByKey(nZone, config))
		{
			m_pRollLog->error("ZoneSitMgr::checkRobotZone zone:%d not exist in csv configure!", nZone);
			return ERROR_CODE::EC_Failed;
		}
		//2.检测现有的变量是否跟配置的数量一致
		this->initZoneData(nZone, config.get_seat_num());
		strRobotCtrl = config.get_room_ctrl();
		if (!strRobotCtrl.empty())
		{
			common_utils::decodeString(strRobotCtrl, vecRobotConfig);
		}
		if (!vecRobotConfig.empty())
		{
			time_t t = time(NULL);
			tm localt;
			localtime_r(&t, &localt);
			for (int i = 0; i < vecRobotConfig.size(); i++)
			{
				//3.如果在配置的时间内则去检测机器选房规则
				if (localt.tm_hour >= vecRobotConfig[i][0] && localt.tm_hour < vecRobotConfig[i][1])
				{
					m_pRollLog->debug("ZoneSitMgr::checkRobotZone hour parameter:%d-%d, %d-%d", nZone, localt.tm_hour, vecRobotConfig[i][0], vecRobotConfig[i][1]);
					//如果上一次设置的机器人规则不一样，则需要重置下机器人选座信息
					bool bCanSet = this->initRobotZoneRule(nZone,vecRobotConfig[i][0]);
					//4.获取现在空闲的房间数量
					if (m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
					{
						for (map<int, ZoneSitData>::iterator iter = m_mapZone2SitInfo[nZone].begin(); iter != m_mapZone2SitInfo[nZone].end(); ++iter)
						{
							if (iter->second.robot == 1 && iter->second.playerCnt == 0)
							{
								iter->second.playerCnt = 4;
								m_mapZone2Num[nZone] += 4;
							}
							if (iter->second.playerCnt == 0 && iter->second.robot == 0)
							{
								vecFreeZone.push_back(iter->first);
								m_pRollLog->debug("ZoneSitMgr::checkRobotZone free zone[zone:%d, sit:%d]", nZone, iter->first);
							}
						}
					}
					//4.设置机器人房间
					if ((vecFreeZone.size() >= vecRobotConfig[i][2]) && (m_mapZoneRobot[nZone].empty()) && (bCanSet))
					{
						struct timeval tpstart;
						gettimeofday(&tpstart,NULL);
						srand(tpstart.tv_usec);
						//取余0会core，1也需要特殊处理
						int nRandNum =  vecRobotConfig[i][3];
						if ((vecRobotConfig[i][2]-vecRobotConfig[i][3]) > 0)
						{
							nRandNum += rand()%(vecRobotConfig[i][2]-vecRobotConfig[i][3] + 1);
						}
						m_pRollLog->debug("ZoneSitMgr::checkRobotZone set robot zone num[zone:%d, num:%d, free size:%d, %d-%d]", nZone, nRandNum, vecFreeZone.size(), vecRobotConfig[i][2], vecRobotConfig[i][3]);
						for (int i = 0; i < nRandNum; i++)
						{
							gettimeofday(&tpstart,NULL);
							srand(tpstart.tv_usec);
							int nRNum = rand()%vecFreeZone.size();
							m_pRollLog->debug("ZoneSitMgr::checkRobotZone set robot zone rand:%d-%d", nRNum, vecFreeZone.size());
							if (nRNum < vecFreeZone.size())
							{
								//4.1.修改房间的状态(人总数),机器人选座数据
								int sit = vecFreeZone[nRNum];
								m_mapZone2SitInfo[nZone][sit].playerCnt = 4;
								m_mapZone2SitInfo[nZone][sit].robot = 1;
								m_mapZone2SitInfo[nZone][sit].updateTime = t;
								m_pRollLog->normal("ZoneSitMgr::checkRobotZone set robot zone[zone:%d, sit:%d, robot:%d, player size:%d]", nZone, sit, m_mapZone2SitInfo[nZone][sit].robot, m_mapZone2SitInfo[nZone][sit].playerCnt);
								vector<int>::iterator iter = find(vecFreeZone.begin(), vecFreeZone.end(), sit);
								if (iter != vecFreeZone.end())
								{
									vecFreeZone.erase(iter);
								}
								if (m_mapZoneRobot.find(nZone) == m_mapZoneRobot.end())
								{
									vector<int> vecTmp;
									m_mapZoneRobot[nZone] = vecTmp;
								}
								if (m_mapZone2Num.find(nZone) == m_mapZone2Num.end())
								{
									m_mapZone2Num[nZone] = 0;
								}
								m_mapZoneRobot[nZone].push_back(sit);
								m_mapZone2Num[nZone] += 4;	
								this->save2Cache(m_mapZone2SitInfo[nZone][sit]);
							}
						}
					}
					//5.释放机器人房间
					else if (vecFreeZone.size() <= vecRobotConfig[i][4])
					{
						if (m_mapZoneRobot.find(nZone) == m_mapZoneRobot.end())
						{
							vector<int> vecTmp;
							m_mapZoneRobot[nZone] = vecTmp;
						}
						if (m_mapZone2Num.find(nZone) == m_mapZone2Num.end())
						{
							m_mapZone2Num[nZone] = 0;
						}

						int nReleaseNum = vecRobotConfig[i][5];
						while( nReleaseNum > 0)
						{
							if (m_mapZoneRobot[nZone].size() > 0)
							{
								struct timeval tpstart;
								gettimeofday(&tpstart,NULL);
								srand(tpstart.tv_usec);
								int nRandNum = rand()%m_mapZoneRobot[nZone].size();
								int sit = m_mapZoneRobot[nZone][nRandNum];
								if (m_mapZone2SitInfo[nZone][sit].robot == 1 && m_mapZone2SitInfo[nZone][sit].playerCnt == 4)
								{
									m_mapZone2SitInfo[nZone][sit].playerCnt = 0;
									m_mapZone2SitInfo[nZone][sit].robot = 0;
									m_mapZone2SitInfo[nZone][sit].updateTime = t;
									m_mapZone2Num[nZone] -= 4;
									this->save2Cache(m_mapZone2SitInfo[nZone][sit]);
								}
								
								vector<int>::iterator iter = find(m_mapZoneRobot[nZone].begin(), m_mapZoneRobot[nZone].end(), sit);
								if (iter != m_mapZoneRobot[nZone].end())
								{
									m_mapZoneRobot[nZone].erase(iter);
								}
								nReleaseNum -= 1;
								m_pRollLog->debug("ZoneSitMgr::checkRobotZone release robot zone[zone:%d, sit:%d, robot size:%d]", nZone, sit, m_mapZoneRobot[nZone].size());
							}
							else
							{
								break;
							}
						}
					}
					break;
				}
			}
		}
		else
		{
			m_pRollLog->error("ZoneSitMgr::checkRobotZone robot configure is empty....");
		}
	}
	catch(...)
	{
		m_pRollLog->error("ZoneSitMgr::checkRobotZone exception.....");
		return ERROR_CODE::EC_Failed;
	}
	return ERROR_CODE::EC_Succeed;
}


bool ZoneSitMgr::isRobotZone(const int &nZone, const int &nSit)
{
	try
	{
		if (m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
		{
			if (m_mapZone2SitInfo[nZone].find(nSit) != m_mapZone2SitInfo[nZone].end())
			{
				if (m_mapZone2SitInfo[nZone][nSit].robot == 1 && m_mapZone2SitInfo[nZone][nSit].playerCnt == 4)
				{
					return true;
				}
			}
		}
	}
	catch(...)
	{
		m_pRollLog->error("ZoneSitMgr::isRobotZone exception.....");
	}	
	return false;
}

bool ZoneSitMgr::passwdValid(const int &nZone, const int &nSit, const string &strPwd)
{
	try
	{
		if (m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
		{
			if (m_mapZone2SitInfo[nZone].find(nSit) != m_mapZone2SitInfo[nZone].end())
			{
				m_pRollLog->normal("ZoneSitMgr::passwdValid parameter[zone:%d, sit:%d, player:%d, setpwd:%d, pwd:%s]", nZone, nSit, m_mapZone2SitInfo[nZone][nSit].playerCnt, m_mapZone2SitInfo[nZone][nSit].nSetPwd, m_mapZone2SitInfo[nZone][nSit].strPwd.c_str());
				if (m_mapZone2SitInfo[nZone][nSit].playerCnt == 0 && m_mapZone2SitInfo[nZone][nSit].nSetPwd == 0)
				{
					m_mapZone2SitInfo[nZone][nSit].strPwd = "";
				}
				if (m_mapZone2SitInfo[nZone][nSit].strPwd.empty())
				{
					if (m_mapZone2SitInfo[nZone][nSit].playerCnt == 0)
					{
						m_mapZone2SitInfo[nZone][nSit].nSetPwd = 0;
					}
					if (m_mapZone2SitInfo[nZone][nSit].nSetPwd == 0)
					{
						m_mapZone2SitInfo[nZone][nSit].nSetPwd = 1;
						m_mapZone2SitInfo[nZone][nSit].strPwd = strPwd;
						m_mapZone2SitInfo[nZone][nSit].nSetPwdTime = time(NULL);
						this->save2Cache(m_mapZone2SitInfo[nZone][nSit]);
						return true;
					}
					else if((time(NULL) - m_mapZone2SitInfo[nZone][nSit].nSetPwdTime) > 2 && m_mapZone2SitInfo[nZone][nSit].playerCnt == 0)
					{
						m_mapZone2SitInfo[nZone][nSit].strPwd = strPwd;
						m_mapZone2SitInfo[nZone][nSit].nSetPwdTime = time(NULL);
						this->save2Cache(m_mapZone2SitInfo[nZone][nSit]);
						return true;
					}
					else if (strPwd.empty()) return true;
				}
				else if (m_mapZone2SitInfo[nZone][nSit].strPwd == strPwd)
				{
					return true;
				}
			}
			if (nSit == 0)
			{
				return true;
			}
		}
	}
	catch(...)
	{
		m_pRollLog->error("ZoneSitMgr::passwdInvalid exception[zone:%d, sit:%d, pwd:%s]", nZone, nSit, strPwd.c_str());
	}
	return false;
}


string ZoneSitMgr::getZoneSitPwd(const int &nZone, const int &nSit)
{
	if (m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
	{
		if (m_mapZone2SitInfo[nZone].find(nSit) != m_mapZone2SitInfo[nZone].end())
		{
			return m_mapZone2SitInfo[nZone][nSit].strPwd;
		}
	}
	return "";
}

int ZoneSitMgr::getZoneSitInfo(int nZone, int nTimeStamp, std::vector<proto29zone::ProtoSitInfo> &vetSitinfo)
{
	//m_pRollLog->debug("ZoneSitMgr::getZoneSitInfo enter");
	int nResult = this->checkRobotZone(nZone);
	if (nResult == ERROR_CODE::EC_Succeed)
	{
		bool bInsert = false;
		if (m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
		{
			for (map<int, ZoneSitData>::iterator iter = m_mapZone2SitInfo[nZone].begin(); iter != m_mapZone2SitInfo[nZone].end(); ++iter)
			{
				bInsert = false;
				if (nTimeStamp == 0 || iter->second.updateTime >= nTimeStamp)
				{
					bInsert = true;
				}
				if (bInsert)
				{
					proto29zone::ProtoSitInfo sitinfo;
					sitinfo.id = iter->second.sit;
					sitinfo.num = iter->second.playerCnt;
					iter->second.strPwd = (iter->second.playerCnt == 0) && (!iter->second.strPwd.empty()) ? "" : iter->second.strPwd;
					sitinfo.nOpenPasswd = iter->second.strPwd.empty() ? 0 : 1;
					m_pRollLog->normal("ZoneSitMgr::getZoneSitInfo result[zone:%d, sit:%d, player:%d, robot:%d, pwd:%d]", nZone, iter->second.sit, iter->second.playerCnt, iter->second.robot, sitinfo.nOpenPasswd);
					vetSitinfo.push_back(sitinfo);
				}
			}
		} 
	}
	return nResult;
}

void ZoneSitMgr::updateZoneSitInfo(int nZone, int nSit, int nPlayerCnt)
{
	int nChgNum = nPlayerCnt; //人数变化量
	int nRobot = 0;
	if(m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
	{
		if (m_mapZone2SitInfo[nZone].find(nSit) != m_mapZone2SitInfo[nZone].end())
		{
			m_pRollLog->normal("ZoneSitMgr::updateZoneSitInfo parameter[zone:%d, sit:%d, player:%d]", nZone, nSit, nPlayerCnt);
			nChgNum = nPlayerCnt - m_mapZone2SitInfo[nZone][nSit].playerCnt;
			m_mapZone2SitInfo[nZone][nSit].playerCnt = nPlayerCnt;
			m_mapZone2SitInfo[nZone][nSit].robot = nRobot;
			m_mapZone2SitInfo[nZone][nSit].updateTime = time(NULL);
			m_mapZone2Num[nZone] += nChgNum;
			if (nPlayerCnt == 0)
			{
				m_mapZone2SitInfo[nZone][nSit].strPwd = "";
				m_mapZone2SitInfo[nZone][nSit].nSetPwd = 0;
			}
			else if (m_mapZone2SitInfo[nZone][nSit].nSetPwd == 0)
			{
				m_mapZone2SitInfo[nZone][nSit].nSetPwd = 1;
			}
			save2Cache(m_mapZone2SitInfo[nZone][nSit]);
			this->checkRobotZone(nZone);
		}
	}
}

void ZoneSitMgr::getZoneState(int nRoomtype, std::vector<proto29zone::ProtoZoneState> &vecZoneState)
{
	m_pRollLog->debug("ZoneSitMgr::getZoneState begin nRoomtype:%d, ",nRoomtype);
	//获取全部，从配置表读取所有区号
	const std::map<int, CsvConfig::ByChooseSeat> byChooseSeatMap = CsvConfigMgr::getInstance().getByChooseSeatMap();
	for(std::map<int, CsvConfig::ByChooseSeat>::const_iterator it = byChooseSeatMap.begin(); it != byChooseSeatMap.end(); it++)
	{
		m_pRollLog->debug("ZoneSitMgr::getZoneState nRoomtype:%d, room_id:%d, id:%d",nRoomtype,it->second.get_room_id(),it->second.get_id());
		if(nRoomtype == it->second.get_room_id())
		{
			proto29zone::ProtoZoneState zoneState;
			getZoneState(it->second.get_id(),zoneState);
			vecZoneState.push_back(zoneState);
		}
	}
}

void ZoneSitMgr::getZoneState(int nZone, proto29zone::ProtoZoneState &zoneState)
{
	m_pRollLog->debug("ZoneSitMgr::getZoneState begin nZone:%d, ",nZone);
	zoneState.zone = nZone;
	if(m_mapZone2Num.find(nZone) != m_mapZone2Num.end())
	{
		m_pRollLog->debug("ZoneSitMgr::getZoneState nZone:%d, num:%d",nZone,m_mapZone2Num[nZone]);
		if(m_mapZone2Num[nZone] <= 100 )
		{

			zoneState.state = ZONESTATE_FLUENT;
		}
		else if(m_mapZone2Num[nZone] <= 150 )
		{

			zoneState.state = ZONESTATE_BUSY;
		}
		else if(m_mapZone2Num[nZone] <= 180 )
		{

			zoneState.state = ZONESTATE_CROWD;
		}
		else// if(m_mapZone2Num[nZone] <= 200 )
		{

			zoneState.state = ZONESTATE_FULL;
		}
	}
	else
	{
		zoneState.state = ZONESTATE_FLUENT;
	}
}

//判断房间座位是否已经满人
bool ZoneSitMgr::isRoomFull(const int &nZone, const int &nSit)
{
	try
	{
		if (m_mapZone2SitInfo.find(nZone) != m_mapZone2SitInfo.end())
		{
			if (m_mapZone2SitInfo[nZone].find(nSit) != m_mapZone2SitInfo[nZone].end())
			{
				if (m_mapZone2SitInfo[nZone][nSit].playerCnt == 4)
				{
					return true;
				}
			}
		}
	}
	catch(...)
	{
		m_pRollLog->error("ZoneSitMgr::isRoomFull exception[zone:%d, sit:%d, pwd:%s]", nZone, nSit);
	}
	return false;
}

